//
//  mcrmSAASRESTService.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 14-5-30.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "RSWRESTService.h"
#import "ProtocolBase.h"
#import "XLGLoginCommonProcess.h"
#import "OnlineUserInfo.h"

@implementation RSWRESTService

-(XLGResult*)getMyInfo:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/user?token=%@", token];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    NSInteger nResult = NO;
    NSError *error = nil;
    
    nResult = r.outResult;
    error = r.outError;
    id outObj = nil;
    if(nResult == YES)
        outObj = r.outObj[@"Value"];
    
    return [XLGResult resultWithOutResult:nResult outObj:outObj outError:error];
}

-(void)modifyMyinfoByInfo:(MyInfo*)myInfo token:(NSString*)token completion:(void(^)(NSInteger nResult,BOOL bResult,NSError *error))completion
{
    NSString *URI =[NSString stringWithFormat:@"rsoa/api/user?email=%@&name=%@&qq=%@&tel=%@&photo=%@&mobile=%@&token=%@",
                    myInfo.email,
                    myInfo.userName,
                    myInfo.qq,
                    myInfo.tel,
                    myInfo.logoURL,
                    myInfo.mobilePhone,
                    token];
    
    [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypePut completion:^(NSInteger nResult, id output, NSError *error)
     {
         BOOL bResult = FALSE;
         if(output != nil)
         {
             NSInteger success = [output[@"Code"] integerValue];
             if(success == YES)
                 bResult = YES;
             else
             {
                 if(error == nil)
                     error = [NSError errorWithDomain:output code:0 userInfo:nil];
             }
         }
         completion(nResult,bResult,error);
     }];
}

-(void)modifyUserHeadImage:(NSString*)logoURL token:(NSString*)token completion:(void(^)(NSInteger nResult,BOOL bResult,NSError *error))completion
{
    NSString *URI =[NSString stringWithFormat:@"rsoa/api/user?photo=%@&token=%@",
                    logoURL,
                    token];
    
    [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypePut completion:^(NSInteger nResult, id output, NSError *error)
     {
         BOOL bResult = FALSE;
         if(output != nil)
         {
             NSInteger success = [output[@"Code"] integerValue];
             if(success == YES)
                 bResult = YES;
             else
             {
                 if(error == nil)
                     error = [NSError errorWithDomain:output code:0 userInfo:nil];
             }
         }
         completion(nResult,bResult,error);
     }];
}

-(void)modifyPasswordWithOldPassword:(NSString*)oldPassword newPassword:(NSString*)newPassword token:(NSString*)token completion:(void(^)(NSInteger nResult,BOOL bResult,NSError *error))completion
{
    NSString *URI = [NSString stringWithFormat:@"rsoa/api/user?newpassword=%@&token=%@&oldpassword=%@", newPassword,token,oldPassword];
    
    NSDictionary *dic = @{@"newpassword":newPassword,
                          @"oldpassword":oldPassword,
                          @"token":token};
    
    [super HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost completion:^(NSInteger nResult, id output, NSError *error)
     {
         BOOL bResult = FALSE;
         if(output != nil)
         {
             NSInteger success = [output[@"Code"] integerValue];
             if(success == YES)
                 bResult = YES;
             else
             {
                 if(error == nil)
                     error = [NSError errorWithDomain:output code:0 userInfo:nil];
             }
         }
         completion(nResult,bResult,error);
     }];
}

-(void)userFeedBackWithFeedContent:(NSString*)content token:(NSString*)token completion:(void(^)(NSInteger nResult,BOOL bResult,NSError *error))completion
{
    NSString *URI = [NSString stringWithFormat:@"rsoa/api/feedback?content=%@&token=%@", content,token];
    NSDictionary *dic = nil;
    
    [super HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost completion:^(NSInteger nResult, id output, NSError *error)
     {
         BOOL bResult = FALSE;
         if(output != nil)
         {
             NSInteger success = [output[@"Code"] integerValue];
             if(success == YES)
                 bResult = YES;
             else
             {
                 if(error == nil)
                     error = [NSError errorWithDomain:output code:0 userInfo:nil];
             }
         }
         completion(nResult,bResult,error);
     }];
}


-(XLGResult*)getOnlineUserList:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/user?token=%@&online=", token];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"List"];
        
        XMSQueueDictionary *qdSet = [OnlineUserInfo JSONObjectProperty:nil];
        
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[OnlineUserInfo class] accordingToColPropertyPair:qdSet];

        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    
    return r;
}

@end
